package com.xxl.job.executor.service.jobhandler;


import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;

import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/7/20  11:47
 * 描述:
 */
@SpringBootTest
public class TestJobapp {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    RestHighLevelClient restHighLevelClient;

    /**
     * 作者: LCG
     * 日期: 2021/7/20  12:34
     * 描述: 创建索引
     * @Param []
     * @Return void
     */
    @Test
    public void createIndex() throws IOException {
        CreateIndexRequest createIndexRequest=new CreateIndexRequest("test123");
        CreateIndexResponse createIndexResponse=restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        String index = createIndexRequest.index();
        System.out.println(index);
    }


    /**
     * 作者: LCG
     * 日期: 2021/7/20  12:38
     * 描述: 判断索引是不是存在
     * @Param []
     * @Return void
     */
    @Test
    public void indexExist() throws IOException {

        GetIndexRequest exist=new GetIndexRequest("test-index1");
        boolean exists=restHighLevelClient.indices().exists(exist, RequestOptions.DEFAULT);

        System.out.println(exists);
    }


    /**
     * 作者: LCG
     * 日期: 2021/7/20  14:16
     * 描述: 添加数据  一个数据
     * @Param []
     * @Return void
     */
    @Test
    public void addData(){

        Map<String,Object> map=new HashMap<>();
        map.put("userName","张三");

        IndexCoordinates coordinates = IndexCoordinates.of("test123");
        elasticsearchRestTemplate.save(map,coordinates);
    }


    /**
     * 作者: LCG
     * 日期: 2021/7/20  14:24
     * 描述: 根据Id更新一个数据
     * @Param []
     * @Return void
     */
    @Test
    public void update() throws IOException {
        UpdateRequest updateRequest=new UpdateRequest();
        Map<String,Object> map=new HashMap<>();
        map.put("userName","站撒-Pfq7k");
        updateRequest.doc(map);
        updateRequest.id("fAqUwnoBRvXrnt-Pfq7k");
        updateRequest.index("test123");

        restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);


    }


    /**
     * 作者: LCG
     * 日期: 2021/7/20  14:52
     * 描述: 查询数据 这是匹配查询类似模糊查询
     * @Param []
     * @Return void
     */
    @Test
    public void query() throws IOException {

        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("userName", "站");

        NativeSearchQuery nativeSearchQuery=new NativeSearchQuery(matchQueryBuilder);

        SearchHits<Map> test123 = elasticsearchRestTemplate.search(nativeSearchQuery, Map.class, IndexCoordinates.of("test123"));

        List<SearchHit<Map>> searchHits = test123.getSearchHits();

        searchHits.forEach(sh->{
            System.out.println(sh.getId());
            System.out.println(sh.getContent().get("userName"));
        });

    }


    /**
     * 作者: LCG
     * 日期: 2021/7/20  15:07
     * 描述: 精确查询  等同于 等于查询
     * @Param []
     * @Return void
     */
    @Test
    public void tearmQuery(){
        //构建精确查询条件
        TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("userName", "站撒-Pfq7k");
        //构建查询
        NativeSearchQuery nativeSearchQuery=new NativeSearchQuery(termsQueryBuilder);
        //执行查询
        SearchHits<Map> test123 = elasticsearchRestTemplate.search(nativeSearchQuery, Map.class, IndexCoordinates.of("test123"));
        //遍历结果
        test123.getSearchHits().forEach(shits->{
            System.out.println("----------"+shits.getId());
            System.out.println("----------"+shits.getContent().get("userName"));
        });
    }


    /**
     * 作者: LCG
     * 日期: 2021/7/20  15:51
     * 描述: 删除索引
     * @Param []
     * @Return void
     */
    @Test
    public void deleeIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("test-index");
        //设置超时
        request.timeout(TimeValue.timeValueMinutes(2));
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
        //返回值是true表示删除成功
        System.out.println(delete.isAcknowledged());
    }

}
